using System;
using System.Configuration;


namespace BIUtilities.Publish.CustomConfiguration
{
    /// <summary>
    /// Parent performancepoint configuration which stores all changes to be made
    /// </summary>
    public class PerformancePointElement :
        ConfigurationElement, IUpdatableConfig
    {
        /// <summary>
        /// The source PPS webservice to read from
        /// </summary>
        [ConfigurationProperty("sourceMonitoringWebservice", IsKey = false, IsRequired = false)]
        public string SourceMonitoringWebservice
        {
            get { return this["sourceMonitoringWebservice"].ToString(); }
            set { this["sourceMonitoringWebservice"] = value; }
        }

        /// <summary>
        /// The source PPS Workspace file to read from
        /// </summary>
        [ConfigurationProperty("sourceWorkspace", IsKey = false, IsRequired = false, DefaultValue="")]
        public string SourceWorkspace
        {
            get { return this["sourceWorkspace"].ToString(); }
            set { this["sourceWorkspace"] = value; }
        }

        /// <summary>
        /// The target PPS webservice to publish changes to
        /// </summary>
        [ConfigurationProperty("targetMonitoringWebservice", IsKey = false, IsRequired = true)]
        public string TargetMonitoringWebservice
        {
            get { return this["targetMonitoringWebservice"].ToString(); }
            set { this["targetMonitoringWebservice"] = value; }
        }

        /// <summary>
        /// Filter the reports and elements on the server by a user specified property.
        /// </summary>
        [ConfigurationProperty("matchOnProperty", IsKey = false, IsRequired = true)]
        public string MatchOnProperty
        {
            get { return this["matchOnProperty"].ToString(); }
            set { this["matchOnProperty"] = value; }
        }

        /// <summary>
        /// If specified, this value must be set for the matched property to be matched.
        /// </summary>
        [ConfigurationProperty("matchOnValue", IsKey = false, IsRequired = false)]
        public string MatchOnValue
        {
            get { return this["matchOnValue"].ToString(); }
            set { this["matchOnValue"] = value; }
        }

        /// <summary>
        /// List of security changes
        /// </summary>
        [ConfigurationProperty("security", IsKey = true, IsRequired = false)]
        public PerformancePointSecurityElementCollection SecurityChanges
        {
            get { return (PerformancePointSecurityElementCollection)this["security"]; }
        }

        /// <summary>
        /// List of datasources
        /// </summary>
        [ConfigurationProperty("dataSources", IsKey = true, IsRequired = false)]
        public PerformancePointDataSourceElementCollection DataSourceChanges
        {
            get { return (PerformancePointDataSourceElementCollection)this["dataSources"]; }
        }

        /// <summary>
        /// List of Excel Report changes
        /// </summary>
        [ConfigurationProperty("excelReports", IsRequired = false)]
        public PerformancePointExcelReportElement ExcelReportChanges
        {
            get { return (PerformancePointExcelReportElement)this["excelReports"]; }
        }

        /// <summary>
        /// List of PPS Pas Report Element changes
        /// </summary>
        [ConfigurationProperty("pasReports", IsRequired = false)]
        public PerformancePointPasReportElement PasReportChanges
        {
            get { return (PerformancePointPasReportElement)this["pasReports"]; }
        }

        /// <summary>
        /// List of PPS Web Page changes
        /// </summary>
        [ConfigurationProperty("webPages", IsRequired = false)]
        public PerformancePointWebPageElement WebPageChanges
        {
            get { return (PerformancePointWebPageElement)this["webPages"]; }
        }

        /// <summary>
        /// List of PPS SSRS changes
        /// </summary>
        [ConfigurationProperty("ssrsReports", IsRequired = false)]
        public PerformancePointSsrsReportElement SsrsReportChanges
        {
            get { return (PerformancePointSsrsReportElement)this["ssrsReports"]; }
        }
        /// <summary>
        /// Whether this element is active and will be processed.
        /// </summary>
        [ConfigurationProperty("enabled", IsKey = false, IsRequired = false, DefaultValue = "true")]
        [RegexStringValidator(@"(^$)|([Tt][Rr][Uu][Ee])|([Ff][Aa][Ll][Ss][Ee])|(\[.*?\])")]
        private string Enabled
        {
            get { return (string)this["enabled"]; }
        }

        /// <summary>
        /// Public parameter exposed for boolean
        /// </summary>
        public bool IsEnabled
        {
            get
            {
                // This might be an injected value, so we try parsing it (and hope it's been replaced)
                // and if not, then we just disable it due to the error.
                bool retVal = false;
                if (bool.TryParse((string)this["enabled"], out retVal))
                {
                    return retVal;
                }
                else
                {
                    throw new ArgumentException(string.Format(
                        "Failed to parse property 'enabled' for {0}",
                        this.ElementInformation.Type.ToString()));
                }
            }
        }

        /// <summary>
        /// Makes this element editable
        /// </summary>
        /// <returns></returns>
        public override bool IsReadOnly()
        {
            return false;
        }


        #region IUpdatableConfig Members
        /// <summary>
        /// Creating a public accessor to get at the property values
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        object IUpdatableConfig.this[string propertyName]
        {
            get { return base[propertyName]; }
            set { base[propertyName] = value; }
        }
        #endregion

    }
}
